﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UTC_Project_Manager.Model.Infrastructure;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for ProjectSettingsControl.xaml
    /// </summary>
    public partial class ProjectSettingsControl : UserControl
    {
        public delegate void PassNewDate(DateTime formerDate, DateTime newDate, String Description);
        public static event PassNewDate newDateEvent;

        private bool isInit;

        
        public ProjectSettingsControl()
        {
            InitializeComponent();
            isInit = true;
            populateFields();
            isInit = false;
        }

        private void populateFields()
        {
            Project currentProject = App.MyConfig.OurProject;
            if (currentProject != null)
            {
                if (currentProject.Name != null)
                    this.projectNameTextBox.Text = currentProject.Name;

                if (currentProject.Description != null)
                    this.descriptionTextBox.Text = currentProject.Description;


                if (currentProject.StartDate != new DateTime(0001,01,01))
                    this.startDatePicker.SelectedDate = currentProject.StartDate;

                if (currentProject.EndDate != new DateTime(0001, 01, 01))
                    this.endDatePicker.SelectedDate = currentProject.EndDate;
            }
        }
                
       
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            Project currentProject = App.MyConfig.OurProject;
            currentProject.Name = this.projectNameTextBox.Text;
            currentProject.Description = this.descriptionTextBox.Text;
            DateTime oldDate ;
            if (this.startDatePicker.SelectedDate != null)
            {
                oldDate = currentProject.StartDate;
                if (oldDate != (DateTime)this.startDatePicker.SelectedDate)
                {
                    currentProject.StartDate = (DateTime)this.startDatePicker.SelectedDate;
                    newDateEvent(oldDate, currentProject.StartDate, "StartDate");
                }
            }
            if (this.startDatePicker.SelectedDate != null)
            {
                oldDate = currentProject.EndDate;
                if (oldDate != (DateTime)this.endDatePicker.SelectedDate)
                {
                    currentProject.EndDate = (DateTime)this.endDatePicker.SelectedDate;
                    newDateEvent(oldDate, currentProject.EndDate, "EndDate");
                }
            }

           disableButtons();

        }

        private void projectNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!isInit)
                enableButtons();
        }

        private void enableButtons()
        {
            this.annulerButton.IsEnabled = true;
            this.saveButton.IsEnabled = true;
        }

        private void disableButtons()
        {
            this.annulerButton.IsEnabled = false;
            this.saveButton.IsEnabled = false;
        }

        private void startDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!isInit)
                enableButtons();
        }

        private void annulerButton_Click(object sender, RoutedEventArgs e)
        {
            Project currentProject = App.MyConfig.OurProject;
            if (currentProject.Name != null)
                this.projectNameTextBox.Text = currentProject.Name;
            else
                this.projectNameTextBox.Text = "";

            if (currentProject.Description != null)
                this.descriptionTextBox.Text = currentProject.Description;
            else
                this.descriptionTextBox.Text = "";

            if (currentProject.StartDate != new DateTime(0001, 01, 01))
                this.startDatePicker.SelectedDate = currentProject.StartDate ;

            if (currentProject.EndDate != new DateTime(0001, 01, 01))
                this.endDatePicker.SelectedDate = currentProject.EndDate ;
            disableButtons();
        }

        
    }
}
